import {addOperationApi, checkApi} from "@/api/operation";
import {format} from "@/utils/format";

export default {
  data() {
    return {
      formData: [],
      searchDate: '',
      numberList: []
    }
  },
  mounted() {
    this.searchDate = this.getToday()
  },
  methods: {
    getToday() {
      return format(undefined, 'YYYY-MM-DD')
    },
    handleSelectionChange(arr) {
      let list = arr.map(item => item.number)
      for (let i = 0; i < list.length; i++) { // 去重
        for (let j = i + 1; j < list.length; j++) {
          if (list[i] === list[j]) {
            list.splice(j, 1);
            j--;
          }
        }
      }
      this.numberList = list
    },
    joinProblem() {
      this.$prompt('请输入问题件原因', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /^[\s\S]*.*[^\s][\s\S]*$/,
        inputErrorMessage: '请输入原因'
      }).then(({value}) => {
        this.submit(this.numberList.join(','), value)
      })
    },
    submit(numbers, value) {
      this.$startLoading()
      // 检测单号
      checkApi({state: 4, numbers: numbers})
        .then(result => {
          if (result.status !== 200) {
            this.$stopLoading()
            return
          }
          let successList = result.resultParam.successList
          if (successList.length === 0) {
            this.$errorMsg('无单号通过检测')
            this.$stopLoading()
            return
          }
          let data = {
            numbers: successList.join(','), // 单号
            state: 4, // 状态(问题件)
            notes: value, // 备注
            laosNotes: '',
            currentUserId: this.$user.id,
            currentUserName: this.$user.username,
            currentName: this.$user.name,
            laosCurrentName: this.$user.laosName,
            sendToUserId: null,
            sendToUserName: '',
            sendToName: '',
            laosSendToName: ''
          }
          // 更新物流信息
          addOperationApi(data).then(result => {
            this.$stopLoading()
            if (result.status !== 200) return
            this.getData()
          }).catch(() => {
            this.$stopLoading()
          })
        })
        .catch(() => {
          this.$stopLoading()
        })
    }
  }
}
